package cn.edu.njuit.children.service.impl;

import cn.edu.njuit.children.entity.Role;
import cn.edu.njuit.children.entity.UserAccount;
import cn.edu.njuit.children.entity.UserRole;
import cn.edu.njuit.children.mapper.RoleMapper;
import cn.edu.njuit.children.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.njuit.children.service.UserService;
import cn.edu.njuit.children.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【sys_user】的数据库操作Service实现
 * @createDate 2022-11-29 08:58:00
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserAccount>
        implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public UserAccount getAccount(String phone) {
        LambdaQueryWrapper<UserAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAccount::getPhone, phone);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public List<Role> getRoleByLogin(String phone) {
        return roleMapper.findByPhone(phone);
    }

    @Override
    public UserAccount regAccount(String phone, String password, String defaultRole) throws Exception {
        //根据登录名查询账户是否存在
        UserAccount user = getAccount(phone);
        if (user != null) {
            throw new Exception("登录名已存在，注册失败");
        }
        user = new UserAccount();
        user.setAccountStatus("1");
        user.setPhone(phone);
        //修改注册时 密码加盐加密格式
        user.setPassword(passwordEncoder.encode(password));
//        account.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        user.setRegisterTime(new Date());
        int insert = this.userMapper.insert(user);
        if (insert > 0) {
            //查询默认角色id，创建账户对应角色
            Role accountRole = this.roleMapper.selectOne(new LambdaQueryWrapper<Role>()
                    .eq(Role::getRoleName, defaultRole));
            if (accountRole == null) {
                //默认角色不存在，创建默认角色。
                accountRole = new Role();
                accountRole.setRoleName(defaultRole);
                this.roleMapper.insert(accountRole);
            }
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(accountRole.getId());
            this.userRoleMapper.insert(userRole);

            return user;
        } else {
            throw new Exception("账户创建失败");
        }
    }

    @Override
    public boolean putPassword(String newPwd) {
        //获取当前登录用户
        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserAccount account = this.getAccount(phone);
        account.setPassword(passwordEncoder.encode(newPwd));
        return userMapper.updateById(account) > 0;
    }
}




